<template>
  <div>
    <div>
      <a-button
        type="primary"
        v-for="item in routers"
        :key="item.url"
        style="margin-right: 30px"
        @click="() => toNav(item)"
      >
        {{ item.name }}
      </a-button>
    </div>
    <div class="router-con">
      <router-view></router-view>
    </div>
  </div>
</template>

<script setup>
import { onMounted, onUpdated, ref, watch, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";

const { currentRoute, push } = useRouter();

const routers = [
  {
    name: "第一个应用",
    url: "/vite/121",
  },
  {
    name: "第二个应用",
    url: "/ceshi2/121",
  },
  {
    name: "第二个应用子应用",
    url: "/ceshi2/page2",
  },
];
const toNav = (item) => {
  // const instance = getCurrentInstance();

  // const router = useRouter();
  // push({
  //   path: item.url,
  //   query: {
  //     time: new Date().getTime(),
  //   },
  // });
  // window.dispatchEvent(new PopStateEvent("popstate", { state: null }));
  // return;
  history.pushState(
    // {
    //   time: new Date().getTime(),
    // },
    null,
    null,
    `#${item.url}`
  );
  window.dispatchEvent(new PopStateEvent("popstate", { state: null }));
  // 主动触发一次popstate事件
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  background: #fff;
  width: 100%;
  /* padding: 10px; */
  box-sizing: border-box;
  display: flex;
}

.menu-con {
  width: 200px;
}

.router-con {
  flex: 1;
}

body {
  margin: 0;
}
</style>
